﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PNProject.Core;
using PNProject.Core.Converter;

namespace PNProject.Controllers
{
    [Authorize(Roles = "SuperAdmin, Censor, Admin")]
    public class AdminBannerController : Controller
    {
        //
        // GET: /AdminBanner/

        public ActionResult Index()
        {
            var list = StaticData.bannerRepo.GetSome(a => a.Type == 0);
            List<BannerModel> data = new List<BannerModel>();
            foreach (var item in list)
            {
                var con = BannerConverter.BannerTo(item);
                data.Add(con);
            }
            BannerContract contract = new BannerContract();
            contract.Title = "Danh sách banner";
            contract.Data = data;
            return View(contract);
        }

        public ActionResult Create()
        {
            return View(new BannerModel());
        }

        [HttpPost]
        public ActionResult Create(BannerModel model, HttpPostedFileBase avatar)
        {
            var obj = BannerConverter.ToBanner(model);
            bool legal = true;
            if (string.IsNullOrEmpty(model.URL))
            {
                //luu file
                if (avatar != null)
                {
                    legal = avatar.ContentType == "image/jpeg" || avatar.ContentType == "image/png" || avatar.ContentType == "image/gif" || avatar.ContentType == "image/jpg";
                    if (legal)
                    {
                        string time = PNLibraries.PNConvertToTimestamp(DateTime.Now).ToString();
                        string newName = time + "-" + avatar.FileName;
                        string path = "/Application_Data/Images/";
                        ServerHelper.SaveFileBaseOnServer(path, avatar, newName);
                        obj.URL = path + newName;
                    }
                }
            }
            if ((avatar != null || !string.IsNullOrEmpty(model.URL)) && legal)
                StaticData.bannerRepo.Save(null, obj);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(string id)
        {
            int newid;
            try
            {
                newid = int.Parse(id);
            }
            catch (Exception e)
            {
                return RedirectToAction("Index");
            }
            var dbo = StaticData.bannerRepo.GetOne(a => a.BannerID == newid);
            var con = BannerConverter.BannerTo(dbo);
            return View(con);
        }

        [HttpPost]
        public ActionResult Edit(BannerModel model, HttpPostedFileBase avatar)
        {
            int newid;
            try
            {
                newid = int.Parse(model.BannerID);
            }
            catch(Exception e)
            {
                return RedirectToAction("Index");
            }
            var obj = BannerConverter.ToBanner(model);
            var dbo = StaticData.bannerRepo.GetOne(a => a.BannerID == newid);
            if(dbo==null)
                return RedirectToAction("Index");
            bool legal = true;
            if (string.IsNullOrEmpty(model.URL))
            {                
                if (avatar != null)
                {
                    legal = avatar.ContentType == "image/jpeg" || avatar.ContentType == "image/png" || avatar.ContentType == "image/gif" || avatar.ContentType == "image/jpg";
                    if (legal)
                    {
                        ServerHelper.DeleteFileOnServer(dbo.URL);
                        string time = PNLibraries.PNConvertToTimestamp(DateTime.Now).ToString();
                        string newName = time + "-" + avatar.FileName;
                        string path = "/Application_Data/Images/";
                        ServerHelper.SaveFileBaseOnServer(path, avatar, newName);
                        obj.URL = path + newName;
                    }
                }
            }
            if ((avatar != null || !string.IsNullOrEmpty(model.URL)) && legal)
            {
                if (dbo.URL != obj.URL)
                    ServerHelper.DeleteFileOnServer(dbo.URL);
                obj.BannerID = dbo.BannerID;
                StaticData.bannerRepo.Save(dbo, obj);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(string id)
        {
            try
            {
                int newid = int.Parse(id);
                var dbo = StaticData.bannerRepo.GetOne(a => a.BannerID == newid);                
                StaticData.bannerRepo.Delete(dbo);
                ServerHelper.DeleteFileOnServer(dbo.URL);
            }
            catch (Exception e)
            {

            }
            return RedirectToAction("Index");
        }

    }
}
